/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.abstractvoid.gui;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
import pl.abstractvoid.languages.LanguageProvider;

/**
 * JDialog responsible for specifying the parameters of adding/removing rows
 * from the TablePanel component. It is invoked via context-sensitive dropdown
 * when right-clicking at the input table.
 * @author Wojciech Szałapski
 * @author Ivo Zieliński
 */
public class AddRemoveRowsQuery extends  javax.swing.JDialog {

    private boolean isDeletion;
    private JTable table;
    private ParameterTableModel tableModel;
    private static final int maximumRowsAdded = 50;

    /**
     * Creates new form DeleteRowsQuery
     *
     * @param isDeletion true if it specifies deletion parameters, false if addition
     * @param table table to be modified
     */
    public AddRemoveRowsQuery(boolean isDeletion, JTable table) {
        setResizable(false);
        this.table = table;
        setModal(true);
        tableModel = (ParameterTableModel) table.getModel();
        this.isDeletion = isDeletion;
        initComponents();
        this.setTitle(LanguageProvider.getInstance().getString("rowsQueryTitle"));
        if (isDeletion) {
            messageBox.setText(LanguageProvider.getInstance().getString("rowsQueryRemoveText"));
        } else {
            messageBox.setText(LanguageProvider.getInstance().getString("rowsQueryAddText"));
        }
        buttonGroup1.add(FromTopRadio);
        buttonGroup1.add(FromBottomRadio);
        FromTopRadio.setSelected(true);
        RowAmountSpecifier.setText("0");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        FromTopRadio = new javax.swing.JRadioButton();
        FromBottomRadio = new javax.swing.JRadioButton();
        MessageBox = new javax.swing.JLabel();
        RowAmountSpecifier = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        messageBox = new javax.swing.JLabel();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        FromTopRadio.setText(LanguageProvider.getInstance().getString("rowsQueryFromTop"));
        FromTopRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FromTopRadioActionPerformed(evt);
            }
        });

        FromBottomRadio.setText(LanguageProvider.getInstance().getString("rowsQueryFromBottom"));
        FromBottomRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FromBottomRadioActionPerformed(evt);
            }
        });

        RowAmountSpecifier.setText("jTextField1");

        jLabel2.setText(LanguageProvider.getInstance().getString("rowsQueryAmount"));

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(LanguageProvider.getInstance().getString("cancel"));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(MessageBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(RowAmountSpecifier, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(FromTopRadio, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(25, 25, 25)
                                .addComponent(FromBottomRadio))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(messageBox, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 19, Short.MAX_VALUE)))
                .addGap(14, 14, 14))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(messageBox)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addComponent(MessageBox, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(RowAmountSpecifier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(FromTopRadio)
                            .addComponent(FromBottomRadio))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(okButton)
                            .addComponent(cancelButton))))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    /**
     * Unused action
     * @param evt 
     */
    private void FromTopRadioActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

    /**
     * Unused action
     * @param evt 
     */
    private void FromBottomRadioActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
    }                                               

    /**
     * Just close the window when cancel is pressed
     * @param evt 
     */
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        dispose();
    }                                            

    /**
     * Execude deletion, addition of rows according to the specified parameters.
     * @param evt 
     */
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        okButton.setEnabled(false);
        try {
            int rowCount = Integer.parseInt(RowAmountSpecifier.getText());
            if (rowCount < tableModel.getRowCount() && rowCount > 0 && isDeletion) {
                tableModel.removeRows(rowCount, FromTopRadio.isSelected());
                table.updateUI();
                dispose();
            } else if (rowCount > maximumRowsAdded && !isDeletion) {
                JOptionPane.showMessageDialog(this, LanguageProvider.getInstance().getString("tooManyRows"), LanguageProvider.getInstance().getString("error"), JOptionPane.ERROR_MESSAGE);
            } else if (rowCount > 0 && !isDeletion) {
                tableModel.addRows(rowCount, FromTopRadio.isSelected());
                table.updateUI();
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, LanguageProvider.getInstance().getString("invalidNumberOfRows"), LanguageProvider.getInstance().getString("error"), JOptionPane.ERROR_MESSAGE);
            }
        } catch (java.lang.NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, LanguageProvider.getInstance().getString("insertAnInteger"), LanguageProvider.getInstance().getString("error"), JOptionPane.ERROR_MESSAGE);
        }
        okButton.setEnabled(true);
    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JRadioButton FromBottomRadio;
    private javax.swing.JRadioButton FromTopRadio;
    private javax.swing.JLabel MessageBox;
    private javax.swing.JTextField RowAmountSpecifier;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel messageBox;
    private javax.swing.JButton okButton;
    // End of variables declaration                   
}
